﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_quan_ly_thuc_don : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllRes();
            GetAllLoaiMon();
        }
    }
    protected void grvLoaiMonAn_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvLoaiMonAn.EditIndex = -1;
        GetAllLoaiMon();
    }
    protected void grvLoaiMonAn_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvLoaiMonAn.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteLoaiMonAn";
            Dal.ParameterCollection = new string[1] { "@LoaiMonID" };
            Dal.ValueCollection = new string[1] { i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllLoaiMon();
        }
        catch { }
    }
    protected void grvLoaiMonAn_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvLoaiMonAn.EditIndex = e.NewEditIndex;
        GetAllLoaiMon();
    }
    protected void grvLoaiMonAn_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvLoaiMonAn.DataKeys[e.RowIndex].Value);
        TextBox txtTenLoaiMon = (TextBox)(grvLoaiMonAn.Rows[e.RowIndex].FindControl("txtLoaiMonAn"));
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateLoaiMonAn";
            Dal.ParameterCollection = new string[2] { "@TenLoaiMon", "@LoaiMonID" };
            Dal.ValueCollection = new string[2] {txtTenLoaiMon.Text,i };
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllLoaiMon();
        }
        catch { }
    }
    protected void GetMenuByNhaHangID()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetMonAnByNhaHangID";
            Dal.ParameterCollection = new string[1] { "@NhaHangID"};
            Dal.ValueCollection = new string[1] { ddlRes.SelectedValue };
            DataTable db = Dal.GetDataTable();
            grvMenu.DataSource = db;
            grvMenu.DataBind();
        }
        catch { }
    }
    protected void grvMenu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvMenu.EditIndex = -1;
        GetMenuByNhaHangID();
    }
    protected void grvMenu_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList ddlLoaiMon = (DropDownList)e.Row.FindControl("ddlLoaiMon");
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllLoaiMonAn";
            DataTable db = Dal.GetDataTable();
            ddlLoaiMon.DataSource = db;
            ddlLoaiMon.DataTextField = db.Columns["TenLoaiMon"].ToString();
            ddlLoaiMon.DataValueField = db.Columns["LoaiMonID"].ToString();
            ddlLoaiMon.DataBind();
        }
        catch { }
        
    }
    protected void grvMenu_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvMenu.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteMonAn";
            Dal.ParameterCollection = new string[1] { "@ThucDonID" };
            Dal.ValueCollection = new string[1] { i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetMenuByNhaHangID();
        }
        catch { }
    }
    protected void grvMenu_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvMenu.EditIndex = e.NewEditIndex;
        GetMenuByNhaHangID();
    }
    protected void grvMenu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvMenu.DataKeys[e.RowIndex].Value);
        TextBox txtTenMon = (TextBox)(grvMenu.Rows[e.RowIndex].FindControl("txtTenMon"));
        DropDownList ddlLoaiMon = (DropDownList)(grvMenu.Rows[e.RowIndex].FindControl("ddlLoaiMon"));
        TextBox txtPrice = (TextBox)(grvMenu.Rows[e.RowIndex].FindControl("txtPrice"));
        TextBox txtDonVi = (TextBox)(grvMenu.Rows[e.RowIndex].FindControl("txtDonVi"));
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateMonAn";
            Dal.ParameterCollection = new string[5] { "@ThucDonID","@LoaiMonID", "@TenMon", "@DonGia", "@DonVi" };
            Dal.ValueCollection = new string[5] { i,ddlLoaiMon.SelectedValue,txtTenMon.Text,txtPrice.Text,txtDonVi.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetMenuByNhaHangID();
        }
        catch { }
    }
    protected void ddlRes_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetLoaiMonByResID();
        GetMenuByNhaHangID();
    }
    protected void GetAllLoaiMon()
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllLoaiMonAn";
            DataTable db = Dal.GetDataTable();
            grvLoaiMonAn.DataSource = db;
            grvLoaiMonAn.DataBind();
            ddlLoaiMon.DataSource = db;
            ddlLoaiMon.DataTextField = db.Columns["TenLoaiMon"].ToString();
            ddlLoaiMon.DataValueField = db.Columns["LoaiMonID"].ToString();
            ddlLoaiMon.DataBind();
        }
        catch { }
    }
    protected void GetAllRes()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetRes";
            DataTable db = Dal.GetDataTable();
            ddlRes.DataSource = db;
            ddlRes.DataTextField = db.Columns["TenNhaHang"].ToString();
            ddlRes.DataValueField = db.Columns["NhaHangID"].ToString();
            ddlRes.DataBind();
        }
        catch { }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_AddLoaiMonAn";
        Dal.ParameterCollection = new string[1] { "@TenLoaiMon"};
        Dal.ValueCollection = new string[1] { txtTenLoaiMon.Text};
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        GetAllLoaiMon();
    }
    protected void btnThemMon_Click(object sender, EventArgs e)
    {
        SQLHelper Dal = new SQLHelper();
        Dal.Open();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_AddMonAn";
        Dal.ParameterCollection = new string[5] { "@NhahangID", "@LoaiMonID", "@TenMon", "@DonGia", "@DonVi" };
        Dal.ValueCollection = new string[5] {ddlRes.SelectedValue,ddlLoaiMon.SelectedValue,txtTenMon.Text,txtPrice.Text,txtDonVi.Text };
        Dal.ExecuteNonQuery();
        Dal.Close();
        Dal.Dispose();
        GetMonAnByLoaiMonIDAndResID();
    }
    protected void GetLoaiMonByResID()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetLoaiMonByResID";
        Dal.ParameterCollection = new string[1] { "@NhaHangID"};
        Dal.ValueCollection = new string[1] { ddlRes.SelectedValue };
        DataTable db=Dal.GetDataTable();
        grvLoaiMonAn.DataSource = db;
        grvLoaiMonAn.DataBind();
    }
    protected void GetMonAnByLoaiMonIDAndResID()
    { 
        SQLHelper Dal=new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetMonAnByLoaiMonIDAndResID";
        Dal.ParameterCollection = new string[2] { "@NhahangID", "@LoaiMonID" };
        Dal.ValueCollection = new string[2] { ddlRes.SelectedValue,ddlLoaiMon.SelectedValue};
        DataTable db = Dal.GetDataTable();
        grvMenu.DataSource = db;
        grvMenu.DataBind();
    }
    protected void ddlLoaiMon_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlRes.SelectedValue!=null)
            GetMonAnByLoaiMonIDAndResID();
    }
}
